package com.linxianqin.springbootVue.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linxianqin.springbootVue.domain.constant.UserConstants;
import com.linxianqin.springbootVue.domain.convert.SysConfigConvert;
import com.linxianqin.springbootVue.domain.dto.SysConfigDTO;
import com.linxianqin.springbootVue.domain.dto.SysRoleDTO;
import com.linxianqin.springbootVue.domain.enums.BusinessType;
import com.linxianqin.springbootVue.domain.model.AjaxResult;
import com.linxianqin.springbootVue.domain.page.TableDataInfo;
import com.linxianqin.springbootVue.domain.query.SysConfigQuery;
import com.linxianqin.springbootVue.manage.annotation.RepeatSubmit;
import com.linxianqin.springbootVue.manage.annotation.SysLog;
import com.linxianqin.springbootVue.manage.core.BaseController;
import com.linxianqin.springbootVue.service.SysConfigService;
import com.linxianqin.springbootVue.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lxq
 * @version 1.0
 * @description: 参数配置 信息操作处理
 * @date 2020/12/17 16:18
 */
@Slf4j
@RestController
@RequestMapping("/system/config")
public class SysConfigController extends BaseController {
    private final SysConfigService configService;
    public SysConfigController(SysConfigService configService){
        this.configService=configService;
    }
    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public AjaxResult getConfigKey(@PathVariable String configKey)
    {
        return AjaxResult.success(configService.selectConfigByKey(configKey));
    }

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysConfigQuery query)
    {
        log.info("查询的信息：{}",query);
        //构建分页bean
        Page<SysConfigDTO> page = buildPage();
        //查询分页数据
        Page<SysConfigDTO> dtoPage = configService.selectConfigPage(page, query);
        return getDataTable(dtoPage);
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable Long configId)
    {
        return AjaxResult.success(SysConfigConvert.INSTANCE.entity2dto(configService.getById(configId)));
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @SysLog(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody SysConfigDTO configDTO)
    {
        //校验参数键名是否唯一
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(configDTO)))
        {
            return AjaxResult.error("新增参数'" + configDTO.getConfigName() + "'失败，参数键名已存在");
        }
        configDTO.setCreateBy(SecurityUtils.getUsername());
        return toAjax(configService.save(SysConfigConvert.INSTANCE.dto2entity(configDTO)));
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @SysLog(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysConfigDTO configDTO)
    {
        if (UserConstants.NOT_UNIQUE.equals(configService.checkConfigKeyUnique(configDTO)))
        {
            return AjaxResult.error("修改参数'" + configDTO.getConfigName() + "'失败，参数键名已存在");
        }
        configDTO.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(configService.updateById(SysConfigConvert.INSTANCE.dto2entity(configDTO)));
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @SysLog(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable List<Long> configIds)
    {
        return toAjax(configService.removeByIds(configIds));
    }

    /**
     * 清空缓存
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @SysLog(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public AjaxResult clearCache()
    {
        configService.clearCache();
        return AjaxResult.success();
    }
}
